package com.zzx.controller;

import com.zzx.domain.tv.AddGoods;
import com.zzx.service.GoodsService;
import com.zzx.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @PreAuthorize("hasAuthority('商品列表')")
    @GetMapping
    public ResponseResult getGoodsList(@RequestParam(required = false) String query, Integer pagenum, Integer pagesize){
        return goodsService.getGoodsList(query,pagenum,pagesize);
    }

    /**
     * 添加商品
     * @param addGoods
     * @return
     */
    @PreAuthorize("hasAuthority('商品列表')")
    @PostMapping
    public ResponseResult addGoods(@RequestBody AddGoods addGoods){
        return goodsService.addGoods(addGoods);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('获取商品详情')")
    @GetMapping("/{id}")
    public ResponseResult getGoodsById(@PathVariable("id") Long id){
        return goodsService.getGoodsById(id);
    }

    /**
     * 根据id删除商品
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('商品删除')")
    @DeleteMapping("/{id}")
    public ResponseResult deleteGoodsById(@PathVariable("id")Long id){
        return goodsService.deleteGoodsById(id);
    }

}
